package EXtension.Controller;

import EXtension.FrameWork.MyDelegate;
import EXtension.Util.FilterImageFIle ;
import EXtension.FrameWork.EventSystem;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.ScrollEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.util.Duration;

import java.io.File;

public class HelloController {

    public double startDragX=0;
    public double startDragY=0;
    public double startTranslateX=0;
    public double startTranslateY=0;
    public Image image;
    public File file;
    public int fileLocation=0;
    public int fileNum=0;
    public File[] files;
    public boolean isSlideShow=false;
    public Timeline timeline;
    @FXML
    private BorderPane borderpane;

    @FXML
    private Button delete;

    @FXML
    private VBox fileChooser;

    @FXML
    private ListView<?> fileList;

    @FXML
    private Button filmstrip;

    @FXML
    private Button folder;

    @FXML
    private Button info;

    @FXML
    private Button next;

    @FXML
    private Button prev;

    @FXML
    private Button rotate;

    @FXML
    private TextField searchBox;

    @FXML
    private Button setting;

    @FXML
    private ImageView showImage;

    @FXML
    private ToolBar toolbar;

    @FXML
    private Button zoomIn;

    @FXML
    private Button zoomOut;

    @FXML
    private Label tips;


    public double getStartDragX() {
        return startDragX;
    }

    public void setStartDragX(double startDragX) {
        this.startDragX = startDragX;
    }

    public double getStartDragY() {
        return startDragY;
    }

    public void setStartDragY(double startDragY) {
        this.startDragY = startDragY;
    }

    public double getStartTranslateX() {
        return startTranslateX;
    }

    public void setStartTranslateX(double startTranslateX) {
        this.startTranslateX = startTranslateX;
    }

    public double getStartTranslateY() {
        return startTranslateY;
    }

    public void setStartTranslateY(double startTranslateY) {
        this.startTranslateY = startTranslateY;
    }

    public BorderPane getBorderpane() {
        return borderpane;
    }

    public void setBorderpane(BorderPane borderpane) {
        this.borderpane = borderpane;
    }

    public Button getDelete() {
        return delete;
    }

    public void setDelete(Button delete) {
        this.delete = delete;
    }

    public VBox getFileChooser() {
        return fileChooser;
    }

    public void setFileChooser(VBox fileChooser) {
        this.fileChooser = fileChooser;
    }

    public ListView<?> getFileList() {
        return fileList;
    }

    public void setFileList(ListView<?> fileList) {
        this.fileList = fileList;
    }

    public Button getFilmstrip() {
        return filmstrip;
    }

    public void setFilmstrip(Button filmstrip) {
        this.filmstrip = filmstrip;
    }

    public Button getFolder() {
        return folder;
    }

    public void setFolder(Button folder) {
        this.folder = folder;
    }

    public Button getInfo() {
        return info;
    }

    public void setInfo(Button info) {
        this.info = info;
    }

    public Button getNext() {
        return next;
    }

    public void setNext(Button next) {
        this.next = next;
    }

    public Button getPrev() {
        return prev;
    }

    public void setPrev(Button prev) {
        this.prev = prev;
    }

    public Button getRotate() {
        return rotate;
    }

    public void setRotate(Button rotate) {
        this.rotate = rotate;
    }

    public TextField getSearchBox() {
        return searchBox;
    }

    public void setSearchBox(TextField searchBox) {
        this.searchBox = searchBox;
    }

    public Button getSetting() {
        return setting;
    }

    public void setSetting(Button setting) {
        this.setting = setting;
    }

    public ImageView getShowImage() {
        return showImage;
    }

    public void setShowImage(ImageView showImage) {
        this.showImage = showImage;
    }

    public ToolBar getToolbar() {
        return toolbar;
    }

    public void setToolbar(ToolBar toolbar) {
        this.toolbar = toolbar;
    }

    public Button getZoomIn() {
        return zoomIn;
    }

    public void setZoomIn(Button zoomIn) {
        this.zoomIn = zoomIn;
    }

    public Button getZoomOut() {
        return zoomOut;
    }

    public Label getTips() {
        return tips;
    }

    public void setTips(Label tips) {
        this.tips = tips;
    }

    public boolean isSlideShow() {
        return isSlideShow;
    }

    public void setSlideShow(boolean slideShow) {
        isSlideShow = slideShow;
    }

    public void initialize(){//初始化
        EventSystem.instance.register(new MyDelegate("clickedImage") {
            @Override
            public void execute(Object data) {
                try{
                    image=new Image(((File)data).getPath());
                    showImage.setImage(image);
                    file=(File)data;
                    files=FilterImageFIle.getImage(file.getParentFile());
                    fileNum=files.length;
                    while (!file.getPath().equals(files[fileLocation].getPath())){
                        fileLocation+=1;
                    }
                    //System.out.println(fileLocation);
                    //searchBox.setText(file.getPath());
                    showInfo(null);
                }
                catch (Exception e){
                    System.out.println("fail"+data);
                }
            }

        });
    }

    public void setZoomOut(Button zoomOut) {
        this.zoomOut = zoomOut;
    }
    @FXML
    void deleteImage(ActionEvent event) {//删除图片
        if(file.delete()){tips.setText(file.getName()+"删除成功！");}
        else {tips.setText(file.getName()+"删除失败!");}
        selectNextImage(null);
        /*TODO：删除后自动跳转到下一张图片或空白图片*/
    }

    @FXML
    void moveImageOnMouseDragged(MouseEvent event) {
        showImage.setTranslateX(startTranslateX+event.getSceneX()-startDragX);
        showImage.setTranslateY(startTranslateY+event.getSceneY()-startDragY);
    }

    @FXML
    void moveImageOnMousePressed(MouseEvent event) {
        startDragX=event.getSceneX();
        startDragY=event.getSceneY();
        startTranslateX=showImage.getTranslateX();
        startTranslateY=showImage.getTranslateY();
    }
    @FXML
    void setFileChooserVisible(ActionEvent event) {//设置左侧目录树是否可见
        if(fileChooser.isDisable()==true){fileChooser.setDisable(false);fileChooser.setVisible(false);}
        else if(fileChooser.isDisable()==false){fileChooser.setDisable(true);fileChooser.setVisible(true);}
        if(fileChooser.isVisible()==true){fileChooser.setVisible(false);}
        else if(fileChooser.isVisible()==false){fileChooser.setVisible(true);}
    }

    @FXML
    void setImageRotation(ActionEvent event) {//顺时针旋转图片
        showImage.setRotate((showImage.getRotate()+90)%360);
        tips.setText("当前图片旋转角度为"+showImage.getRotate());
    }

    @FXML
    void showInfo(ActionEvent event) {//显示详细信息
        tips.setText(file.getName()+"（第"+(fileLocation+1)+"张图片）");
    }

    @FXML
    void setImageSize(ScrollEvent event){//滚轮调整大小
        if(event.getDeltaY()>0){zoominImage(null);}
        else{zoomoutImage(null);}
    }
    @FXML
    void zoominImage(ActionEvent event) {//放大图片
        showImage.setPreserveRatio(true);
        if(showImage.getFitWidth()<=borderpane.getPrefWidth()){
            showImage.setFitWidth(showImage.getFitWidth()*1.1);
        }
    }

    @FXML
    void zoomoutImage(ActionEvent event) {//缩小图片
        showImage.setPreserveRatio(true);
        showImage.setFitWidth(showImage.getFitWidth()*0.9);
    }

    @FXML
    void startSlideShow(ActionEvent event){//切换幻灯片模式
        if(isSlideShow()){
            setSlideShow(false);tips.setText("幻灯片停止播放！");
            timeline.stop();
        }
        else{
            setSlideShow(true);tips.setText("幻灯片开始播放！");
            timeline=new Timeline(new KeyFrame(Duration.seconds(1),(ActionEvent e)->{
                if(isSlideShow()){
                    selectNextImage(null);
                }
            }));
            timeline.setCycleCount(Timeline.INDEFINITE);
            timeline.play();
        }
    }

    @FXML
    void selectNextImage(ActionEvent event){//切换下一张图片
        fileLocation++;
        fileLocation=fileLocation%fileNum;
        file=files[fileLocation];
        image=new Image(file.getPath());
        showImage.setImage(image);
        showInfo(null);
    }

    @FXML
    void selectPrevImage(ActionEvent event){//切换上一张图片
        fileLocation--;
        if(fileLocation<0){fileLocation=fileNum-1;}
        file=files[fileLocation];
        image=new Image(file.getPath());
        showImage.setImage(image);
        showInfo(null);
    }
    /*
    TODO:待办事项
    √1、幻灯片播放模块
    √2、鼠标滚轮调整大小
    √3、调整大小fixup
    √4.界面自适应
    5.详细信息
    6.按钮互动
    7.目录树的实现（可选）
    */
}
